package net.alcuria.ball;

import java.util.List;

import net.alcuria.framework.Game;
import net.alcuria.framework.Graphics;
import net.alcuria.framework.Input.TouchEvent;
import net.alcuria.framework.Screen;

public class MainMenuScreen extends Screen {
	boolean pressingButton;
	
	public MainMenuScreen(Game game){
		super(game);
		pressingButton = false;
		if(Settings.soundEnabled){
			Assets.elysium.play();
		}
	}

	public void update(float deltaTime) {

		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		int len = touchEvents.size();
		for(int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if(event.type == TouchEvent.TOUCH_DOWN) {	
				// toggle the sound enabled key
				if(inBounds(event, 5, 420, 64, 64)) {
					Settings.soundEnabled = !Settings.soundEnabled;
					if(Settings.soundEnabled){
						Assets.click.play(1);
						Assets.elysium.play();
					} else {
						Assets.elysium.stop();
					}
				}
				
				// if we're in bounds to one of the main buttons, we can jump to that screen
				if(inBounds(event, 0, 0, 544, 320) ) {
					pressingButton = true;
					if(Settings.soundEnabled)
						Assets.click.play(1);
					return;
				}
			}
			if(event.type == TouchEvent.TOUCH_UP){
				if(inBounds(event, 0, 0, 544, 320) ) {
					Assets.map = new Map("map/testmap.cmf", game.getFileIO().getAssets());
					game.setScreen(new MapViewerScreen(game));
					//Assets.world1.play();
					
				}
				pressingButton = false;
			}
		}
	}

	private boolean inBounds(TouchEvent event, int x, int y, int width, int height) {
		if(event.x > x && event.x < x + width - 1 &&
				event.y > y && event.y < y + height - 1)
			return true;
		else
			return false;
	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		
		g.drawPixmap(Assets.background, 0, 0);
		if(!pressingButton){
			g.drawPixmap(Assets.button, 400, 140, 0, 0, 235, 79);
		} else {
			g.drawPixmap(Assets.button, 400, 140, 0, 79, 235, 79);
		}
		
		// draw other assets
		if(Settings.soundEnabled) {
			g.drawPixmap(Assets.sound, 5, 420, 0, 0, 64, 64);
		}
		else {
			g.drawPixmap(Assets.sound, 5, 420, 0, 64, 64, 64);
		}
	}

	@Override
	public void pause() {
		Settings.save(game.getFileIO());
		Assets.elysium.pause();

	}

	@Override
	public void resume() {
		if (Settings.soundEnabled) {
			Assets.elysium.play();
		}
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		Assets.elysium.stop();
	}


}
